package com.zzy.openrequest.chatservice;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzy.openrequest.config.ChatConfig;
import com.zzy.openrequest.dto.Prompt;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class ChatService {

    @Value("${chatai.base-url}")
    private String baseUrl;

    @Value("${chatai.api-key}")
    private String apiKey;

    @Value("${chatai.model}")
    private String model;

    public JSONObject call(Prompt prompt) {


        HttpRequest post = HttpUtil.createPost(baseUrl);
        HashMap<String, String> headMap = new HashMap<>();
        headMap.put("Authorization", apiKey);
        headMap.put("Content-Type", "application/json");
        post.addHeaders(headMap);
        prompt.setModel(model);
        post.body(JSONUtil.toJsonStr(prompt));
        post.timeout(160000);
        HttpResponse response = post.execute();
        String body = response.body();
        return JSONUtil.parseObj(body);

    }


}
